package com.lorenzogao.pley.ui.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.lorenzogao.pley.AppApplication
import com.lorenzogao.pley.R
import com.lorenzogao.pley.ui.BaseView
import com.lorenzogao.pley.ui.di.component.AppComponent
import com.lorenzogao.pley.ui.presenter.BasePresenter
import javax.inject.Inject

/**
 * 作者：Lorenzo Gao
 * Date: 2018/1/2
 * Time: 16:13
 * 邮箱：2508719070@qq.com
 * Description:
 */

abstract class ProgressFragment<T : BasePresenter<*, *>> : Fragment(), BaseView {


    private var mRootView: FrameLayout? = null

    private var mViewProgress: View? = null
    private var mViewContent: FrameLayout? = null
    private var mViewEmpty: View? = null


    private var mTextError: TextView? = null

     lateinit var mApplication: AppApplication

    private lateinit var realContentView: View
    @Inject
    lateinit var mPresenter: T


    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {


        mRootView = inflater!!.inflate(R.layout.fragment_progress, container, false) as FrameLayout
        mViewProgress = mRootView!!.findViewById(R.id.view_progress)
        mViewContent = mRootView!!.findViewById<View>(R.id.view_content) as FrameLayout
        mViewEmpty = mRootView!!.findViewById(R.id.view_empty)

        mTextError = mRootView!!.findViewById<View>(R.id.text_tip) as TextView

        mViewEmpty!!.setOnClickListener { onEmptyViewClick() }


        return mRootView

    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        this.mApplication = activity.application as AppApplication
        setupActivityComponent(mApplication.getAppComponent())
        setRealContentView()

        initView()
    }

    fun onEmptyViewClick() {

    }

    private fun setRealContentView() {
         realContentView = LayoutInflater.from(activity).inflate(setLayout(), mViewContent, true)
    }

    fun getContentView(): View {

        return realContentView
    }

    fun showProgressView() {
        showView(R.id.view_progress)

    }

    fun showContentView() {

        showView(R.id.view_content)
    }

    fun showEmptyView() {


        showView(R.id.view_empty)
    }


    fun showEmptyView(resId: Int) {


        showEmptyView()
        mTextError!!.setText(resId)
    }

    fun showEmptyView(msg: String) {


        showEmptyView()
        mTextError!!.text = msg
    }


    fun showView(viewId: Int) {

        for (i in 0 until mRootView!!.childCount) {

            if (mRootView!!.getChildAt(i).id == viewId) {

                mRootView!!.getChildAt(i).visibility = View.VISIBLE
            } else {
                mRootView!!.getChildAt(i).visibility = View.GONE
            }

        }


    }

    override fun showLodading() {
        showProgressView()
    }

    override fun dimissLoading() {
        showContentView()
    }


    override fun showError(msg: String) {

        showEmptyView(msg)
    }


    abstract fun initView()

    abstract fun setLayout(): Int

    abstract fun setupActivityComponent(appComponent: AppComponent)

}